package com.sumainfor.pc.dev.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.dev.dao.TenantIndexMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.TenantIndexService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Jack Chen
 * @Create 2019-02-22 14:32
 * 首页模块服务
 */
@Service
public class TenantIndexServiceImpl implements TenantIndexService {

    @Autowired
    private TenantIndexMapper indexMapper;

    @Autowired
    private ZpcMktFeign zpcMktFeign;

    /**
     * 获得首页Banner 列表数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getIndexBanner() {
        List<Map<String, Object>> list = indexMapper.selectIndexBanner(CustomMap.create("state", "0"));
        return list;
    }

    /**
     * 获得首页热门房源信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getHotHousingIndexData() {
        //zc修改代码
        List<Map<String,Object>> allList = indexMapper.selectHotHousingData(null);
        List<String> HouInfoIdList = new ArrayList<>();
        Map<String, Object> zpcmktMap = new HashMap<>();
        if (allList.size() > 0 && allList != null) {
            for (Map<String, Object> getHotHousingAllMap : allList) {
                HouInfoIdList.add(MapUtils.getString(getHotHousingAllMap, "HouInfoId"));
            }
            zpcmktMap.put("HouInfoIdList", HouInfoIdList);
        }
        JsonResult zpcMktResult = zpcMktFeign.getHouInfoIdState(zpcmktMap);
        List<Map<String, Object>> zpcMktList = (List<Map<String, Object>>) zpcMktResult.get("result");
        if (zpcMktList.size() > 0 && zpcMktList != null) {
            for (Map<String, Object> map1 : allList) {
                for (Map<String, Object> map2 : zpcMktList) {
                    if (MapUtils.getString(map1, "HouInfoId") != null && MapUtils.getString(map2, "HouInfoId") != null && MapUtils.getString(map1, "HouInfoId").equals(MapUtils.getString(map2, "HouInfoId"))) {
                        map1.putAll(map2);
                        System.out.println("map" + map1);
                    }
                }
            }
        }
        //移除待上架的热门房源，首页热门房源就从筛选过后的热门房源里面找到
        for (int a = 0; a < allList.size(); a++) {
            if (MapUtils.getInteger(allList.get(a), "State") == 0||MapUtils.getInteger(allList.get(a),"State")==2) {
                allList.remove(a);
            }
        }
         //获取首页热门房源
        List<Map<String,Object>> shouyeList = new ArrayList<>();
        //非首页热门房源信息
        List<Map<String,Object>> shoutenoList = new ArrayList<>();

        for (Map<String,Object> allMap:allList) {
        if (MapUtils.getInteger(allMap,"IfPopular") == 1){
            shouyeList.add(allMap);
        }else{
            shoutenoList.add(allMap);
        }
        }

        List<Map<String,Object>> newList = new ArrayList<>();
        int count = shouyeList.size();
        final  int normCount = 4;
        if (count<normCount){
            //不足四个
            for (int i=0;i<shoutenoList.size();i++){
                shouyeList.add(shoutenoList.get(i));
            }
            if (shouyeList.size()>4){
                 newList = shouyeList.subList(0, 4);

            }else{
                 newList = shouyeList;
            }
        }


    return newList;

//        int count = indexMapper.selectHotHousingDataCount();
//        final int normCount = 4;
//        List<Map<String, Object>> data = indexMapper.selectHotHousingData(CustomMap.create("type", 1));
//        if (count < normCount) {
//            //不足4个
//            List<Map<String, Object>> list = indexMapper.selectHotHousingData(CustomMap.create("type", 0)
//                    .put("limit", normCount - count));
//            data.addAll(list);
//        }
//
//        return data;
    }

    /**
     * 获得所有的热门房源信息数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getHotHousingAll() {
        List<Map<String, Object>> getHotHousingAllList = indexMapper.selectHotHousingData(null);
        List<String> HouInfoIdList = new ArrayList<>();
        Map<String, Object> zpcmktMap = new HashMap<>();
        if (getHotHousingAllList.size() > 0 && getHotHousingAllList != null) {
            for (Map<String, Object> getHotHousingAllMap : getHotHousingAllList) {
                HouInfoIdList.add(MapUtils.getString(getHotHousingAllMap, "HouInfoId"));
            }
            zpcmktMap.put("HouInfoIdList", HouInfoIdList);
        }

        //zc修改代码
        JsonResult zpcMktResult = zpcMktFeign.getHouInfoIdState(zpcmktMap);
        List<Map<String, Object>> zpcMktList = (List<Map<String, Object>>) zpcMktResult.get("result");
        if (zpcMktList.size() > 0 && zpcMktList != null) {
            for (Map<String, Object> map1 : getHotHousingAllList) {
                for (Map<String, Object> map2 : zpcMktList) {
                    if (MapUtils.getString(map1, "HouInfoId") != null && MapUtils.getString(map2, "HouInfoId") != null && MapUtils.getString(map1, "HouInfoId").equals(MapUtils.getString(map2, "HouInfoId"))) {
                        map1.putAll(map2);
                        System.out.println("map" + map1);
                    }
                }
            }
        }
      //移除待上架的房源，不能在热门房源里面展示
        for (int a = 0; a < getHotHousingAllList.size(); a++) {
            if (MapUtils.getInteger(getHotHousingAllList.get(a), "State") == 0||MapUtils.getInteger(getHotHousingAllList.get(a),"State")==2) {
                getHotHousingAllList.remove(a);
            }
        }
        return getHotHousingAllList;
    }

    /**
     * 获得租房攻略
     *
     * @param page
     * @return
     */
    @Override
    public IPage<Map<String, Object>> getRaidersMainList(Page page) {
        return indexMapper.selectRaidersData(page);
    }

    /**
     * 根据租房攻略主编号查询所有的内部房源
     *
     * @param raId
     * @return
     */
    @Override
    public IPage<Map<String, Object>> getRaidersInfoDataByRaId(Page page, String raId) {
        return indexMapper.selectRaidersHousingInfo(page, raId);
    }
}
